﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class ChiTietNoiHoc
    {
        public String maChiTietNoiHoc;
        public String maCheDoHoc;
        public String maTieuLuan;
        public String maTotNghiep;


        private String tenTruong;
        private String tenThanhPho;
        private String nganhHoc;
        private DateTime thoiGianBatDau;
        private DateTime thoiGianKetThuc;
        

        //object
        public TieuLuan tieuLuan
        {
            get
            {
                if (maTieuLuan != "")
                    return new TieuLuan(maTieuLuan);
                return null;

            }
            set
            {
                tieuLuan = value;
            }
        }
        public TotNghiep totNghiep
        {
            get
            {
                if (maTotNghiep != "")
                    return new TotNghiep(maTotNghiep);
                return null;
            }
            set
            {
                totNghiep = value;
            }
        }
        public CheDoHoc cheDoHoc
        {
            get
            {
                return CheDoHoc.cheDoHocFormID(maCheDoHoc);
            }
            set
            {
                cheDoHoc = value;
            }
        }

        //database
        Database myDatabase = Database.shareDatabase;

        public ChiTietNoiHoc(String _tenTruong, String _tenThanhPho, String _nganhHoc, DateTime _thoigianBatDau, DateTime _thoiGianKetThuc,TieuLuan _tieuLuan,TotNghiep _totNghiep,CheDoHoc _cheDoHoc)
        {
            maChiTietNoiHoc = this.getMaChiTietNoiHocFromDatabase();

            //properties
            tenTruong = _tenTruong;
            tenThanhPho = _tenThanhPho;
            nganhHoc = _nganhHoc;
            thoiGianBatDau = _thoigianBatDau;
            thoiGianKetThuc = _thoiGianKetThuc;

            

            // ID
            maCheDoHoc = cheDoHoc.maCheDoHoc;
            if (tieuLuan != null)
                maTieuLuan = tieuLuan.maTieuLuan;
            else
                maTieuLuan = "";
            if (totNghiep != null)

                maTotNghiep = totNghiep.maTotNghiep;
            else
                maTotNghiep = "";

            // object
            tieuLuan = _tieuLuan;
            totNghiep = _totNghiep;
            cheDoHoc = _cheDoHoc;
        }
        public ChiTietNoiHoc(String _maChiTietNoiHoc)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM ChiTietNoiHoc WHERE MaChiTietNoiHoc = '" + _maChiTietNoiHoc + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                maChiTietNoiHoc = table.Rows[0]["MaChiTietNoiHoc"].ToString();
                tenTruong = table.Rows[0]["TenTruong"].ToString();
                tenThanhPho = table.Rows[0]["TenThanhPho"].ToString();
                nganhHoc = table.Rows[0]["NganhHoc"].ToString();
                maCheDoHoc = table.Rows[0]["MaCheDoHoc"].ToString();
                thoiGianBatDau = table.Rows[0].Field<DateTime>("ThoiGianBatDau");
                thoiGianKetThuc = table.Rows[0].Field<DateTime>("ThoiGianKetThuc");
                maTieuLuan = table.Rows[0]["MaTieuLuan"].ToString();
                maTotNghiep = table.Rows[0]["MaTotNghiep"].ToString();
            }
        }
        private String getMaChiTietNoiHocFromDatabase()
        {
            String x;
            x = Database.getID("ChiTietNoiHoc", "MaChiTietNoiHoc");
            return x;
        }
        public void save()
        {
            String sqlChiTietNoiHoc = "INSERT INTO ChiTietNoiHoc VALUES('" + maChiTietNoiHoc + "','" + tenTruong + "','" + tenThanhPho + "','" + nganhHoc + "','" + maCheDoHoc + "','" + thoiGianBatDau.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + thoiGianKetThuc.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + maTieuLuan + "','" + maTotNghiep + "')";
            QueryResult result = myDatabase.Query(sqlChiTietNoiHoc);
            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi khi lưu dữ liệu vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else // save successfully
            {
                tieuLuan.save();
                totNghiep.save();
            }
        }
        public void update()
        {
            bool isDupplicated = Database.isDupplicate("ChiTietNoiHoc", "MaChiTietNoiHoc", maChiTietNoiHoc);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE ChiTietNoiHoc SET TenTruong = '" + tenTruong + "', TenThanhPho = '" + tenThanhPho + "',MaCheDoHoc = '" + maCheDoHoc + "', NganhHoc = '" + nganhHoc + "',ThoiGianBatDau = '" + thoiGianBatDau.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', ThoiGianKetThuc = '" + thoiGianKetThuc.Date.ToString("yyyy-MM-dd HH:mm:ss")
                    + "' WHERE MaChiTietNoiHoc = '" + maChiTietNoiHoc + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi update liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {

                }

            }
        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM ChiTietNoiHoc WHERE MaChiTietNoiHoc = '" + maChiTietNoiHoc + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                tieuLuan.delete();
                totNghiep.delete();
            }
        }
    }
}
